package com.unittec.drainage.provider.manage.service;

import com.unittec.drainage.provider.common.Md5;
import com.unittec.drainage.provider.scope.ThreadLocalScope;
import com.unittec.drainage.provider.service.HystrixRestTemplateService;
import com.unittec.drainage.sdk.auth.domain.SysUserInfo;
import com.unittec.drainage.sdk.auth.request.Req101135;
import com.unittec.drainage.sdk.auth.response.Res101135;
import com.unittec.drainage.sdk.root.common.FieldConstants;
import com.unittec.drainage.sdk.root.domain.GatewayRequest;
import com.unittec.drainage.sdk.root.domain.Result;
import com.unittec.drainage.sdk.root.internal.exception.BusinessException;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户服务
 *
 * @author wangyongfeng
 * @date 2021/8/24
 */
@Service
public class UserService {

    @Resource
    private HystrixRestTemplateService hystrixRestTemplateService;

    //远程调用101服务返回Id-用户列表
    public List<SysUserInfo> getUserList(GatewayRequest<Req101135> req, Req101135 req101135){
        req.setMethod("101135");
        req.setContent(req101135);
        Result<Res101135> result = hystrixRestTemplateService.exchange(Md5.getMd5(FieldConstants.SYS_REQUEST_SOURCE_ID),
                "http://drainage-auth-101/101135", req, ThreadLocalScope.getAuthData(),
                new ParameterizedTypeReference<Result<Res101135>>() {});
        if(result.isSuccess()){
            //return result.getData().getList().stream().map(sysUserRes -> sysUserRes.getId()).collect(Collectors.toList());
            return result.getData().getList();
        }
        throw new BusinessException(result.getBizMsg());
    }
}
